package com.abc.pro.widget;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.widget.PopupWindow;

import com.abc.pro.util.DisplayUtils;

public class PopupView {
	
	protected final Context context;
	protected final PopupWindow window;
	private Drawable background = null;
	protected final WindowManager windowManager;
	private View root = null;
	private View anchor;
	
	private final int POPUP_VIEW_IN_DP_WIDTH = 250;
	private final int POPUP_VIEW_IN_DP_HEIGHT = 250;
	
	public PopupView(View anchor) {
		if (anchor == null) {
			throw new NullPointerException("The context for PopupCalendar cannot be empty or null!");
		}
		this.anchor = anchor;
		this.context = this.anchor.getContext();
		this.window = new PopupWindow(context);
		
		// context.getResources().getDimensionPixelSize(R.dimen.your_dimen_res);
		
		// when a touch even happens outside of the window
		// make the window go away
		window.setTouchInterceptor(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
					PopupView.this.window.dismiss();
					return true;
				}
				return false;
			}
		});

		windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
	}
	
	/**
	 * Sets the content view. Probably should be called from {@link onCreate}
	 * 
	 * @param root
	 *            the view the popup will display
	 */
	public void setContentView(View root) {
		this.root = root;
		this.root.setVisibility(View.VISIBLE);
		
		window.setContentView(root);
	}
	
	/**
	 * Will inflate and set the view from a resource id
	 * 
	 * @param layoutResID
	 */
	public void setContentView(int layoutResID) {
		LayoutInflater inflator =
				(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		setContentView(inflator.inflate(layoutResID, null));
	}
	
	public void preShow() {
		int winWidth = DisplayUtils.getActualSize(this.context, POPUP_VIEW_IN_DP_WIDTH);
		int winHeight = DisplayUtils.getActualSize(this.context, POPUP_VIEW_IN_DP_HEIGHT);
		window.setWidth(winWidth);
		// window.setHeight(winHeight/*WindowManager.LayoutParams.WRAP_CONTENT*/);
		window.setHeight(-2);//wrap_content
		window.setTouchable(true);
		window.setFocusable(true);
		window.setOutsideTouchable(true);

		if (root == null) {
			throw new NullPointerException("setContentView() was not called with a view to display.");
		}
		
		if (background == null) {
			window.setBackgroundDrawable(new BitmapDrawable());
		} else {
			window.setBackgroundDrawable(background);
		}
		
		window.setContentView(root);
	}
	
	public void show() {
		preShow();
		
		int rootWidth 		= DisplayUtils.getActualSize(this.context, POPUP_VIEW_IN_DP_WIDTH);//root.getMeasuredWidth();
		int rootHeight 		= root.getMeasuredHeight();

		DisplayMetrics displaymetrics = new DisplayMetrics();
		windowManager.getDefaultDisplay().getMetrics(displaymetrics);
    	int screenHeight = displaymetrics.heightPixels;
    	int screenWidth = displaymetrics.widthPixels;
    	
		int xPos 			= (screenWidth - rootWidth) / 2;
		// int yPos	 		= DisplayUtils.getActualSize(this.context, 70);
		int yPos	 		= (screenHeight - rootHeight) /4;
		
		window.showAtLocation(this.anchor, Gravity.NO_GRAVITY, xPos, yPos);
	}
	
	public void dismiss(){
	    window.dismiss();
	}
}
